package com.xuecheng.content.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.web.exception.BusinessException;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Handler;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author HuangYuwu
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private MinioTemplate minioTemplate;

    /**
     * 课程信息分页查询
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/course/list")
    public ResponseResult findByPage(PageRequest request, @RequestBody CourseDto dto){
        return courseBaseService.findByPage(request,dto);
    }

    /**
     * 课程图片上传
     * @param uploadFile
     * @return
     */
    @PostMapping("/course/upload")
    public ResponseResult upload(MultipartFile uploadFile) throws IOException {
        String url = minioTemplate.upload(uploadFile.getInputStream(),
                uploadFile.getOriginalFilename(), uploadFile.getContentType());
        return ResponseResult.okResult(url);
    }

    /**
     * 新增课程信息
     * @param courseBaseDto
     * @return
     */
    @PostMapping("/course")
    public ResponseResult saveCourseBase(@RequestBody CourseBaseDto courseBaseDto){
        return courseBaseService.saveCourseBase(courseBaseDto);
    }

    /**
     * 根据课程id查询课程信息（回显）
     * @param courseBaseId
     * @return
     */
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult findCourseBaseById(@PathVariable Long courseBaseId){
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        CourseBaseVo courseBaseVo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        return ResponseResult.okResult(courseBaseVo);
    }

    /**
     * 修改课程信息
     * @param dto
     * @return
     */
    @PutMapping("/course")
    public ResponseResult updateCourseBase(@RequestBody CourseBaseDto dto){
        return courseBaseService.updateCourseBase(dto);
    }

    /**
     * 根据课程id删除课程信息
     * @param courseId
     * @return
     */
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteCourseBaseById(@PathVariable Long courseId) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return courseBaseService.deleteCourseBaseById(courseId);
    }

    /**
     * 机构提交课程审核
     * @param courseBaseId
     * @return
     */
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult commitCourseBase(@PathVariable Long courseBaseId){
        return courseBaseService.commitCourseBase(courseBaseId);
    }

    /**
     * 管理员审核课程
     * @return
     */
    @PostMapping("/course/approve")
    public ResponseResult approveCourseBase(@RequestBody CourseAuditDto courseAuditDto){
        return courseBaseService.approveCourseBase(courseAuditDto);
    }
}
